using GameServer.ShootingGame.WeaponsClasses;
using GameServer.ShootingGame.Physics.CollisionReactionClasses.Base;
using System.Drawing;
using GameServer.ShootingGame.Physics.CollisionReactionClasses;
using System.Collections.Generic;
using GameServer.ShootingGame.WeaponsClasses.LaunchProfiles;
using System;
namespace GameServer.ShootingGame.RenderableClasses
{

    abstract public class Ordinance : MobileObject
    {
        private int liveTime;
        private int liveTimeRemaining;
        public double defaultSpeed;
        public GameObject owner;
        protected LaunchProfiles launchProfile;
        public PointF source;
        public PointF target;
        public WeaponInfo weaponInfo;

        public Ordinance()
        {
            owner = null;
            isHitDetectionEnabled = true;
            source = PointF.Empty;
            target = PointF.Empty;
            Facing = 0;
            defaultSpeed = 0;
            //speed = defaultspeed * travelSpeedModifier;
            Speed = 0;
            VelocityDirection = 0;
            LiveTime = 0;
            weaponInfo = null;
            collisionEffects = null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="owner"></param>
        /// <param name="weaponInfo"></param>
        /// <param name="travelDirection">Default = 0</param>
        /// <param name="defaultSpeed">Default = 1</param>
        /// <param name="travelSpeedModifier">Default = 1</param>
        /// <param name="liveTime">Default = 5000</param>
        /// <param name="max_Speed">Default = 0</param>
        /// <param name="acceleration">Default = 0</param>
        /// <param name="deceleration">Default = 0</param>
        public Ordinance(GameObject owner, WeaponInfo weaponInfo)
        {
            this.owner = owner;
            launchProfile = weaponInfo.launchprofile;
            source = PointF.Empty;
            target = PointF.Empty;
            Facing = 0;
            defaultSpeed = 0;
            //speed = defaultspeed * travelSpeedModifier;
            Speed = weaponInfo.ordinanceSpeed;
            VelocityDirection = 0;
            LiveTime = liveTime;
            this.weaponInfo = weaponInfo;
            collisionEffects = null;
            //Math.cos(travelDirection/180*Math.PI) * travelSpeed, Math.sin(travelDirection/180*Math.PI) * travelSpeed
        }

        override public void Update()
        {
            if (IsExpired)
                return;
            LiveTimeRemaining -= 1;
            //			if(memoryX >= GamePlayManager.stageWidth || memoryX <= 0 || memoryY >= GamePlayManager.stageHeight || memoryY <= 0){
            //				isExpired = true;
            //			}
            if (LiveTimeRemaining <= 0)
            {
                IsExpired = true;
            }
            if (!IsExpired)
                base.Update();
        }

        override protected void UpdateThrust()
        {
            LaunchProfile.UpdateTrajectory(launchProfile, this, source, target);
        }


        /// <summary>
        /// Adds the object to the update list. Returns false if the object is already on the list.
        /// </summary>
        override public bool AddObject()
        {
            if (!GamePlayManager.OrdinanceList.Contains(this))
            {
                GamePlayManager.OrdinanceList.Add(this);
                return true;
            }
            return false;
        }

        /// <summary>
        /// Removes the object from the update list. Returns false if the object was not on the update list.
        /// </summary>
        /// <returns></returns>
        override public bool RemoveObject()
        {
            if (GamePlayManager.OrdinanceList.Contains(this))
            {
                GamePlayManager.OrdinanceList.Remove(this);
                return true;
            }
            return false;
        }

        public int LiveTime
        {
            get
            {
                return liveTime;
            }
            set
            {
                liveTime = value;
                LiveTimeRemaining = liveTime;
            }
        }

        public int LiveTimeRemaining
        {
            get
            {
                return liveTimeRemaining;
            }
            set
            {
                liveTimeRemaining = value;
                if (liveTimeRemaining < 0)
                    liveTimeRemaining = 0;
            }
        }

        override public bool IsExpired
        {
            set
            {
                base.IsExpired = value;
                if (IsExpired)
                {
                    //if (model.collideAnimation)
                    //{
                    //}
                }
            }
        }

        override public RectangleF CollidablePart
        {
            get
            {
                if (collidablePart == null)
                {
                    collidablePart = new RectangleF(Convert.ToSingle(xPosition + relativeCollidablePointX * Math.Cos(Facing)), Convert.ToSingle(yPosition + relativeCollidablePointY * Math.Sin(Facing)), Convert.ToSingle(width), Convert.ToSingle(height));
                }
                else
                {
                    collidablePart.X = Convert.ToSingle(xPosition + relativeCollidablePointX);
                    collidablePart.Y = Convert.ToSingle(yPosition + relativeCollidablePointY);
                }
                return collidablePart;
            }
            set
            {
                collidablePart = value;
            }
        }

        virtual public Ordinance Clone()
        {
            return null;
        }

        public override GamePlayManager GamePlayManager
        {
            get
            {
                return base.GamePlayManager;
            }
            set
            {
                base.GamePlayManager = value;
                id = value.nextOrdinanceItemID++;
            }
        }

    }
}
